package com.imyours.webservice.services;

import org.hibernate.Session;

import com.imyours.model.team.TeamEntity;
import com.imyours.model.user.UserEntity;
import com.imyours.model.util.HibernateUtil;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

@Service(name = "removeTeam")
public class RemoveTeam extends ContextService {

	public RemoveTeam(Context context) {
		super(context);
	}

	public void execute() {
		UserEntity user = getUserEntity();
		if (user != null) {
			String id = context.getParameter().get("id");
			Session se = HibernateUtil.getSessionFactory().openSession();
			TeamEntity teamEntity = (TeamEntity) se.get(TeamEntity.class, Long.parseLong(id));
			if (teamEntity != null) {
				if (teamEntity.getUser().getId() == user.getId()) {
					se.beginTransaction();
					se.delete(teamEntity);
					se.getTransaction().commit();
					se.close();
					context.success();
				}
			}
		}

	}

}
